package OusloginRegister;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;

@WebServlet(name = "OusLogin",urlPatterns = "/ouslogin")
public class OusLogin extends HttpServlet {
    java.sql.Connection conn = null;
    String strConn;
    java.sql.Statement sqlStmt = null;
    ResultSet sqlRst = null;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       try {
           Class.forName("org.h2.Driver");
           conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
           sqlStmt = conn.createStatement();
           if(request.getParameter("submit")!=null)
           {
               String phone = request.getParameter("PHONE");
               String password = request.getParameter("PASSWORD");
               String sql = "SELECT * FROM CUSTOMER  WHERE PHONE="+"'"+phone+"'";
               sqlRst=sqlStmt.executeQuery(sql);
               if(sqlRst.next())
               {
                   if(sqlRst.getString("PASSWORD").equals(password))
                   {
                       request.getSession().setAttribute("NAME",sqlRst.getString("NAME"));
                       request.getSession().setAttribute("PHONE",sqlRst.getString("PHONE"));
                       System.out.println("登陆成功！");
                       response.sendRedirect("http://localhost:8084/hotel.html");
//                       response.sendRedirect("https://www.baidu.com");
                   }
               }
               else
               {
                   System.out.println("账号或密码错误！");
                   response.sendRedirect("http://localhost:8084/index.jsp");
               }
           }
       }
       catch (Exception ex)
       {
           System.out.println(ex.toString());
       }
}

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
